package com.hxzy.controller.common;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.hxzy.common.constans.RedisConstans;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.vo.R;
import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.GifCaptcha;
import com.wf.captcha.SpecCaptcha;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Api(tags = "图片验证码API")
@RestController
@RequestMapping(value = "/common/captcha")
public class CaptchaController {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 过期时间 (如果用户没有配置，给一个默认值为5)
     */
    @Value(value = "${captcha.expired:5}")
    private Long expired;

    /**
     * 验证码的类型
     */
    @Value(value = "${catcha.type:airth}")
    private String type;


    @ApiOperation(value = "图片验证码")
    @GetMapping(produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public R captcha(@RequestParam(value = "width",required = false,defaultValue = "150") int width,
                     @RequestParam(value = "height",required = false,defaultValue = "50") int height,
                     @RequestParam(value = "len",required = false,defaultValue = "4") int len){
        if(this.type.equals("png")){
            return this.png(width,height,len);
        }else if(this.type.equals("gif")){
            return this.gif(width,height,len);
        }else{
            return this.airth(width,height,2);
        }
    }


    private R png( int width, int height,int len) {
        SpecCaptcha captcha = new SpecCaptcha(width, height);
        captcha.setLen(len);
        //返回结果
        String text = captcha.text();
        String img = captcha.toBase64();
        String uuid = IdUtil.fastUUID();

        // 改为使用redis来存储
        String redisKey = RedisConstans.getRedisKey(RedisConstans.CAPTCHA_KEY, uuid);
        this.redisTemplate.opsForValue().set(redisKey, text, expired, TimeUnit.MINUTES);

        //返回给前端
        Map<String, Object> data = new HashMap<>();
        data.put("img", img);
        data.put("uuid", uuid);
        return R.okHasData(data);
    }


    private R gif(int width,int height,int len) {
        GifCaptcha captcha = new GifCaptcha(width, height);
        captcha.setLen(len);
        //返回结果
        String text = captcha.text();
        String img = captcha.toBase64();
        String uuid = IdUtil.fastUUID();

        String redisKey = RedisConstans.getRedisKey(RedisConstans.CAPTCHA_KEY, uuid);
        this.redisTemplate.opsForValue().set(redisKey, text, expired, TimeUnit.MINUTES);

        //返回给前端
        Map<String, Object> data = new HashMap<>();
        data.put("img", img);
        data.put("uuid", uuid);
        return R.okHasData(data);
    }


    private R airth(int width, int height,  int len) {

        ArithmeticCaptcha captcha = new ArithmeticCaptcha(width, height);
        captcha.setLen(len);
        //返回结果
        String text = captcha.text();
        String img = captcha.toBase64();
        String uuid = IdUtil.fastUUID();

        String redisKey = RedisConstans.getRedisKey(RedisConstans.CAPTCHA_KEY, uuid);
        this.redisTemplate.opsForValue().set(redisKey, text, expired, TimeUnit.MINUTES);

        //返回给前端
        Map<String, Object> data = new HashMap<>();
        data.put("img", img);
        data.put("uuid", uuid);
        return R.okHasData(data);
    }

}
